package org.springsource.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springsource.domain.Account;
import org.springsource.domain.AccountCategory;
import org.springsource.domain.Car;
import org.springsource.domain.Category;
import org.springsource.services.AccountService;
import org.springsource.services.CategoryService;


@Controller
@RequestMapping("/accounts")
public class AccountController {
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private Car Ferrari;
	private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		//model.addAttribute("accounts", this.accountService.getAccounts());
		model.addAttribute(new Account());
		return "list";
	}
	@ExceptionHandler
    @ResponseStatus(value = HttpStatus.OK)
	@ResponseBody
    public String handleMethodArgumentNotValidException(
            MethodArgumentNotValidException error) {
		BindingResult res = error.getBindingResult();
		logger.info("obj name"+res.getObjectName());
		List<ObjectError> err =  res.getAllErrors();
		
		for(ObjectError er : err){
			logger.info(er.getDefaultMessage());
			logger.info(er.getCode());
		}
		return "Bad request: " + error.getMessage();
        
    }
	@Transactional
	@RequestMapping(value="add",method = RequestMethod.POST,produces="application/json")
	public @ResponseBody
	String create(@RequestBody @Valid Account account) {
			/*System.out.println(account);
			Car c = new Car();
			c.setMake("ferrari");
			c.setModel("asasasas");
			account.setCars(new ArrayList<Car>());
			account.getCars().add(c);
			//account.setCars(set);
*/			this.accountService.saveOrUpdate(account);
			System.out.println(this.accountService.getAccounts().size());
			return "not";
		
	}
	@Transactional
	@RequestMapping(value="json",method = RequestMethod.GET,produces="application/json")
	public @ResponseBody String listJson() {
		logger.info("in json");
		//Category catAdmin = this.categoryService.getCategory(new Long(2));
		Category catAdmin = new Category();
		catAdmin.setName("users");
		
		catAdmin = this.categoryService.saveCategory(catAdmin);
		
		Account ac  = this.accountService.getAccount(new Long(1));
		Account nickolay  = new Account();
		nickolay.setName("martin");
		nickolay.setEmail("martin@mail.com");
		
		AccountCategory acCat = new AccountCategory();
		acCat.setAccount(nickolay);
		acCat.setCategory(catAdmin);
		nickolay.getAccountCategories().add(acCat);
		logger.info("email:"+ac.getEmail());
		
		this.accountService.saveOrUpdate(nickolay);
		logger.info("new inserted:"+nickolay.getId().toString());
		List<Car> cars = ac.getCars();
		for(Car c : cars){
			logger.info(c.getModel());
		}
		Set<AccountCategory> cat = ac.getAccountCategories();
		for(AccountCategory a : cat){
			logger.info("createdBy:"+a.getCreatedBy());
		}
		return "success";
		//return this.accountService.getAccounts();
		//return new ArrayList<Account>();
	}
}
